package com.lagou.service.impl;

import com.lagou.dao.RoleDao;
import com.lagou.domain.*;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAllRole(Role role) {
        List<Role> list = roleDao.findAllRole(role);
        return list;
    }

    @Override
    public void saveRole(Role role) {
        Date date = new Date();
        role.setCreatedTime(date);
        role.setUpdatedTime(date);
        roleDao.saveRole(role);
    }

    @Override
    public void updateRole(Role role) {
        role.setUpdatedTime(new Date());
        roleDao.updateRole(role);
    }

    @Override
    public List<Menu> findSubMenuListByPid(int pid) {
        List<Menu> menuList = roleDao.findSubMenuListByPid(pid);
        return menuList;
    }

    @Override
    public List<String> findMenuByRoleId(int roleId) {
        List<String> list = roleDao.findMenuByRoleId(roleId);
        return list;
    }

    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {
        int roleId = roleMenuVo.getRoleId();
        roleDao.deleteRoleContextMenu(roleId);
        Date date = new Date();
        for(Integer menuId : roleMenuVo.getMenuIdList()){
            Role_menu_relation role_menu_relation = new Role_menu_relation();
            role_menu_relation.setRoleId(roleId);
            role_menu_relation.setMenuId(menuId);
            role_menu_relation.setCreatedTime(date);
            role_menu_relation.setUpdatedTime(date);
            role_menu_relation.setCreatedBy("system");
            role_menu_relation.setUpdatedBy("system");
            roleDao.roleContextMenu(role_menu_relation);
        }
    }

    @Override
    public void deleteRole(int id) {
        roleDao.deleteRoleContextMenu(id);
        roleDao.deleteRoleContextResource(id);
        roleDao.deleteRole(id);
    }

    @Override
    public List<ResourceCategory> findResourceListByRoleId(int id) {
        List<Integer> list = roleDao.findResourceIdRoleId(id);
        List<ResourceCategory> categoryList = roleDao.findResourceCategoryById(list);
        for(ResourceCategory category : categoryList){
            ResourceCategoryVo resourceCategoryVo = new ResourceCategoryVo();
            resourceCategoryVo.setCategoryId(category.getId());
            resourceCategoryVo.setResourceIdList(list);
            List<Resource> resourceList = roleDao.findResourceByCategoryIdAndRid(resourceCategoryVo);
            category.setResourceList(resourceList);
        }
        return categoryList;
    }

    @Override
    public void roleContextResource(RoleResourceVo roleResourceVo) {
        int roleId = roleResourceVo.getRoleId();
        roleDao.deleteRoleContextResource(roleId);
        for(Integer resourceId : roleResourceVo.getResourceIdList()){
            Role_Resource_Relation role_resource_relation = new Role_Resource_Relation();
            role_resource_relation.setRoleId(roleId);
            role_resource_relation.setResourceId(resourceId);
            Date date = new Date();
            role_resource_relation.setCreatedTime(date);
            role_resource_relation.setUpdatedTime(date);
            role_resource_relation.setCreatedBy("system");
            role_resource_relation.setUpdatedBy("system");
            roleDao.roleContextResource(role_resource_relation);
        }
    }
}
